<?php

namespace Home\Controller;

use Think\Controller;
// 指定允许其他域名访问  
header('Access-Control-Allow-Origin:*');  
// 响应类型  
header('Access-Control-Allow-Methods:*');  
// 响应头设置  
header('Access-Control-Allow-Headers:x-requested-with,content-type');  
class MoneyController extends Controller
{

    public function mymoney()
    {
        if(!IS_POST)
        {
            datamsg(0,'非法操作');
        }
        $user_id=I("user_id");
        $token=I("token");
        $status=I("status");
        if(empty($status))
        {
            datamsg('1','缺少参数');
        }
        $user=user_cinfo($user_id,$token);
        // $openid =D('wxuser')->getFieldBy($user_id, 'openid');

        $list=D('money_log')->field('create_time,user_id,price')->where('user_id='.$user_id.' and status='.$status)->select();

        if(empty($list))
        {
            datamsg('1','暂无更多数据！');
        }else
        {
            foreach ($list as $k => $v)
            {
                $list[$k]['create_time']=date("Y-m-d H:i");
            }
        }

        datamsg('1',"成功",$list);
    }


    //提现
    public function withdraw()
    {
        if(!IS_POST)
        {
            datamsg(0,'非法操作');
        }
        $user_id=I("user_id");
        $token=I("token");
        $user=user_cinfo($user_id,$token);
        $nickname= D('siji as a')->field('a.xingming,b.yue')->join('hy_wxuser as b on a.openid=b.openid')->where('a.openid="'.$user['openid'].'" and a.status=1')->find();
       
        datamsg('1','成功',$nickname);
    }

    /**
     * 查询周一之前可提现的余额
     * @return [type] [description]
     */
    public function seleWeek()
    {
        !IS_POST && datamsg(0,'非法操作');
        $data  = I();
        $user=user_cinfo($data['user_id'],$data['token']);
        
        //3天前的时间
        $money  = getWeek2($user);   

        $total_price['ratio'] = $money['xianshangbili'];
        $total_price['price'] = $money['ketixian'];
        $total_price['fuwufei'] = $money['fuwufei'];

        datamsg(1,'成功',$total_price);
    }
    //申请提现
    public function sub_tixian()
    {
        !IS_POST && datamsg(0,'非法操作');
        
        $ratio =D('user')->where(array('id'=>1))->getField('tixianfeilv');
        
        $today  = date("w");
        // $today!=3 && datamsg(0,'周三才可以提现');
        
        $datas   = I();
        $user_id=I("user_id");
        $token=I("token");
        $user=user_cinfo($user_id,$token);

        //周日前的时间戳
        $endToday=getWeek2($user);   
       
        $total['price']=$endToday['ketixian'];


        $total['price']>$user['yue'] && datamsg(0,'上周余额已经提现了');

        $rules = array(
             array('card','require','银行卡号必须！',1), //默认情况下用正则进行验证
             array('money','require','提现金额必须！',1),
        );

        $wxuser = M("wxuser"); 
        if (!$wxuser->validate($rules)->create()){
            datamsg(0,$wxuser->getError());
        }
        $datas['money']<=0 && datamsg(0,'没有可提现余额');

        $data['price']=I('post.money');
        $data['card']=I('post.card');

        if($user['yue']<$data['price'])
        {
            datamsg(0,'暂时没有这么多余额可提现！');
        }
        $line=D('withdraw')->where('uid='.$user_id.' and status=1')->find();

        if(!empty($line))
        {
            datamsg(0,'已经有金额提现在处理');
        }
        
        $data['status']='3';
        $data['tx_status']='1';
        $data['create_time']=time();
        $data['user_id']=$user_id;
        $data['desc']="编号为".$user_id."用户提现，提现金额:".I('money');
        $res= D('money_log')->add($data);

        $result = array('uid'=>$datas['user_id'],'money'=>$datas['money'],'status'=>1,'create_time'=>time(),'name'=>$user['tel'],'card'=>$data['card'],'ratio'=>$datas['ratio']);

        $idss    = D('withdraw')->add($result);

        $endToday=getWeek();   
        $res_total=D('xllog')->field('id,orderid')->where('openid="'.$user['openid'].'" and status=6 and addtime<='.$endToday.' and withdraw_status=0')->select();
        
        if(!empty($res_total))
        {
            foreach ($res_total as $k => $v) {
                $order_id[] = $v['id'];
            }
            $withdraw_log   = array(
                'withdraw_id'   => $idss,
                'order'         => json_encode($order_id),
                'create_time'   => time(),
            );

            D('withdraw_log')->add($withdraw_log);
        }else
        {

        }
            


        if(!$res)
        {
            datamsg('0','系统繁忙，请稍后再试');
        }


        datamsg('1','成功');
    }
    //提现进度
    public function tixian_jindu()
    {
        if(!IS_POST)
        {
            datamsg(0,'非法操作');
        }
        $user_id=I("user_id");
        $token=I("token");
        $logid=I("logid");
        $user=user_cinfo($user_id,$token);

        if(empty($logid))
        {
            datamsg('0','参数错误！');
        }
        $info= D('money_log')->field('tx_status,status')->find($logid);
        if(empty($info) || $info['status']!='3')
        {
            datamsg('0','找不到该提现数据!');
        }
        unset($info['status']);
        if($info['tx_status']=='3')
        {
            $info['tx_status']='拒绝提现';
        }
        if($info['tx_status']=='2')
        {
            $info['tx_status']='通过提现,请耐心等待金额到账';
        }
        if($info['tx_status']=='1')
        {
            $info['tx_status']='正在审核，请稍等';
        }
        datamsg('1','成功',$info);
    }
        
    //提现方式
    public function xj_money()
    {
        if(!IS_POST)
        {
            datamsg(0,'非法操作');
        }
        $user_id=I("user_id");
        $token=I("token");
        $orderid=I("orderid");
        $user=user_cinfo($user_id,$token);

        if(empty($orderid))
        {
            datamsg('0','参数错误');
        }

        $res= D('xllog')->find($orderid);
       if(empty($res))
       {        
          datamsg('0','找不到该订单！');
       }
       $data['pay_statu']=1;
       $data['pay_status_dk']=1;
       $data['status']=6;
       $data['id']=$orderid;

       $res=D('xllog')->save($data);
       if(empty($res))
       {
        datamsg('0','系统繁忙，请稍后再试');
       }
        datamsg('1','成功');
    }   

}
